function authsub_init() {
  google.gdata.client.init(handleError);

  var token = google.accounts.user.checkLogin(EVENT_FEED_URL);

  myService = 
      new google.gdata.calendar.CalendarService(APP_NAME);
  if (google.accounts.user.checkLogin(EVENT_FEED_URL)) {
    $("#order_form").show();
    $("#login_status").append("<h3>已登入<a href='manage_order.html'>請到此登出</a></h3> ");
  } else {
    $("#order_form").hide();
    $("#login_status").append("<h3>未登入<a href='manage_order.html'>請到此登入</a></h3> ");
  }
}

function create_order(order_div) {
  //console.log(order_div.text());
  var entry = new google.gdata.calendar.CalendarEventEntry();
  var currTime = new Date();
  var gTitle = google.gdata.Text.create(ESTORE_HEADER+ "飲料訂單" 
    +currTime.getHours()+":"+currTime.getMinutes());
  entry.setTitle(gTitle);
  var when = new google.gdata.When();
  var startTime = new google.gdata.DateTime(currTime);
  startTime.setDateOnly();
  var currTime2 = new Date();
  var endTime = new google.gdata.DateTime(currTime2);
  endTime.setDateOnly();
  when.setStartTime(startTime);
  when.setEndTime(endTime);

  var email = $("#send_address > input").val();
  var who = new google.gdata.Who();
  //console.log(who);
  who.setEmail(email);

  //console.log(entry);
  entry.addParticipant(who);

  var notifyProp = new google.gdata.calendar.SendEventNotificationsProperty();
  notifyProp.setValue(true);
  entry.setSendEventNotifications(notifyProp);

  var modifyAble = new google.gdata.calendar.GuestsCanModifyProperty();
  modifyAble.setValue(true);
  entry.setGuestsCanModify(notifyProp);

  var notes = "50藍飲料訂單\n";

  var total = 0;
  order_div.find("tbody > tr").each(function(){
    var sub_total = parseInt($(this).find("td:eq(5)").text());
    total += sub_total;
    $(this).find("td").each(function() {
      notes += $(this).text() + " ";
    });
    notes += "\n";
  });

  notes += "總計:" + total;
  //console.log(notes);
  var content = new google.gdata.atom.Text.create(notes);
  entry.setContent(content);

  var callback = function(result) {
    $("#login_status").empty().append("<h3>訂單送出成功!<a href='manage_order.html'>到此查看</a></h3> ");
  }

  var handleError = function(error) {
    $("#login_status").empty().append("<h1>訂單送出 '失敗'!</h1>");
  }
  myService.insertEntry(EVENT_FEED_URL, entry, callback, 
    handleError, google.gdata.calendar.CalendarEventEntry);
}
